//
//  ViewController.m
//  20-JSON作业
//
//  Created by mafeng on 16/8/10.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "FindCar.h"
#import "Brand.h"
#import "Brandlist.h"
#import "Result.h"
#import "BrandTableViewCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)FindCar *findCar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
}

- (void)handleData {
    // 读取json文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"CarBrand" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    // 赋值给model
    self.findCar = [FindCar modelWithDic:dict];
    
}

// tableview的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _findCar.result.brandlist[section].list.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _findCar.result.brandlist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carBrandCellReuse"];
    // 取到要用的model
    Brand *brand = self.findCar.result.brandlist[indexPath.section].list[indexPath.row];
    cell.myLabel.text = brand.name;
    return cell;
}
  // 返回分区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.findCar.result.brandlist[section].letter;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
